//
//  RootViewController.m
//  UI06_UIScrollView
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 YYL. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    创建scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 600)];
    scrollView.backgroundColor = [UIColor lightGrayColor];
//    设置可以偏移的范围
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3, 0);
    
    scrollView.tag = 1001;
//    整页偏移  默认为NO
    scrollView.pagingEnabled = YES;
//    关闭水平滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
//    关闭回弹效果
    scrollView.bounces = NO;
//    改变初始偏移量
//    scrollView.contentOffset = CGPointMake(100, 0);
//    设置代理人
    scrollView.delegate = self;
//    scrollView.bouncesZoom = YES;
//    [scrollView setZoomScale:0.5 animated:YES];
    
//    缩放最大比例
//    scrollView.maximumZoomScale = 2.0;
//    缩放最小比例
//    scrollView.minimumZoomScale = 0.5;
//
    scrollView.zoomScale = 1.0;

    for (int i = 0; i < 3; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
//        创建专门用于放大缩小的scrollView
        UIScrollView *little = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollView.frame.size.width * i, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        
//        设置放大和缩小的倍数
        little.maximumZoomScale = 2;
        little.minimumZoomScale = 0.5;
        
//        设置代理人
        little.delegate = self;
        
        NSString *string = [NSString stringWithFormat:@"%d", i];
        [imageView setImage:[UIImage imageNamed:string]];
        [string release];
        
        [little addSubview:imageView];
        [scrollView addSubview:little];
        [little release];
        [imageView release];
    }

    [self.view addSubview:scrollView];
    [scrollView release];
    
//    创建UIpageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 625, self.view.frame.size.width, 20)];
    pageControl.backgroundColor = [UIColor redColor];
//    设置点的数量
    pageControl.numberOfPages = 3;
//    添加事件
    [pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    pageControl.tag = 1002;
    
    
    [self.view addSubview:pageControl];
    [pageControl release];
    
}

#pragma mark - UIScrollVIewDelegate
// 获取缩放视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
 
    return [scrollView.subviews firstObject];

    
}


#pragma mark - UIScrollViewDelegate
// 滚动(产生偏移)就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%f", scrollView.contentOffset.x);
    
////    获取pageControl
//    UIPageControl *pageControl = [self.view viewWithTag:1002];
////    通过坐标来确定在pageControl.currentPage的值
//    pageControl.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
   
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"将要拖拽");
}
// 将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"将要结束拖拽");
}

// scrollViewDidEndDecelerating - 已经结束减速
// scrollViewWillBeginDecelerating - 将要开始减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //    获取pageControl
    UIPageControl *pageControl = [self.view viewWithTag:1002];
    //    通过坐标来确定在pageControl.currentPage的值
    pageControl.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
    
    
    for (UIScrollView *little in scrollView.subviews) {
        
        //        判断对象是否属于一个类型
        if ([little isKindOfClass:[UIScrollView class]]) {
            //          将视图比例还原
            little.zoomScale = 1.0;
            
        }
        
        
    }
}

#pragma mark - pageControl
- (void)pageControlAction:(UIPageControl *)pageControl {
    NSLog(@"%ld", pageControl.currentPage);
//    通过tag获取scrollView
    UIScrollView *scroolView = [self.view viewWithTag:1001];
//    通过第几个点设置scroolView的偏移量
//    使用具有动画的方式
    [scroolView setContentOffset:CGPointMake(scroolView.frame.size.width * pageControl.currentPage, 0) animated:YES];
//    从滚动过的scrollView找到其它子视图
    for (UIScrollView *little in scroolView.subviews) {
        
//        判断对象是否属于一个类型
        if ([little isKindOfClass:[UIScrollView class]]) {
//          将视图比例还原
            little.zoomScale = 1.0;
            
        }
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
